<template>
  <div class="login">
    <!-- 顶部导航 -->
    <itemnavbar />
    <!-- logo -->
    <ul>
      <li class="to1">
        <van-image
          width="10rem"
          height="10rem"
          fit="contain"
          src="/logo_bj.png"
        />
      </li>
      <li class="to1">
        <van-image
          round
          width="5rem"
          height="5rem"
          src="https://img01.yzcdn.cn/vant/cat.jpeg"
        />
      </li>
    </ul>
    <!-- 输入区域 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div class="denglu" style="margin: 16px">
        <van-button
          round
          block
          native-type="submit"
          style="background: #e4004f; color: #fff; line-height: 30px"
          >立即登录</van-button
        >
      </div>
    </van-form>
    <!-- 底部导航 -->
    <van-row type="flex" justify="space-around">
      <van-col span="6">
        <router-link to="/login">忘记密码?</router-link>
      </van-col>
      <van-col span="6"></van-col>
      <van-col span="6">
        <router-link to="/regist">立即注册</router-link>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import Itemnavbar from "../components/Itemnavbar.vue";
import { mapMutations } from "vuex";
export default {
  components: { Itemnavbar },
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    // 获取更新用户登录缓存函数 userLogin
    ...mapMutations(["userLogin"]),
    // 发送登录请求
    onSubmit() {
      let url = "/user/login";
      let params = `user_name=${this.username}&password=${this.password}`;
      this.axios
        .post(url, params)
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            console.log(res.data.result);
            this.userLogin(res.data.result);
            this.$router.go(-1);
          } else if (res.data.code == 201) {
            console.log("用户名或密码错误");
          } else {
            console.log("未知错误");
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
};
</script>

<style scoped>
ul {
  text-align: center;
  position: relative;
}

ul > li:nth-child(2) {
  z-index: 10;
  position: absolute;
  top: 120px;
  left: 50%;
  margin-left: -40px;
}
.van-form {
  margin-top: 100px;
  position: absolute;
  padding-left: 10%;
  color: #222222 !important;
}
.van-row {
  padding-left: 27px;
  margin-top: 320px;
  color: #7c7c7c;
  font-size: 14px;
}
</style>
